package com.app.business.config.aspect;

import com.app.system.log4j2.LogUtils;
import com.app.system.utils.WebUtils;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 存储业务日志（不涉及系统权限后台管理）
 * Created by wcf-pc on 2018/5/6.
 */
@Aspect
@Component
@SuppressWarnings("all")
public class BusinessLogAspect {
    private Logger logger = LogUtils.getBusinessLogger();

    @Pointcut("execution(* com.app.business.web..*(..))")
    public void webLog() {
    }

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求，记录请求内容
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//
//        // 记录下请求内容
//        logger.info("============================================");
//        String classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
//        logger.info("CLASS_METHOD：" + classMethod);
//
//        Object username = SecurityUtils.getSubject().getPrincipal();//用户名
//        String url = request.getRequestURL().toString();
//        String uri = request.getQueryString();
//        String ip = WebUtils.getIpAddr(request);
//        String method = request.getMethod();
//        String out = ip + " | " + username + " | " + url + "?" + uri + " | " + method;
//        logger.info(out);


    }

    @AfterReturning(returning = "ret", pointcut = "webLog()")
    public void doAfterReturning(Object ret) throws Throwable {
        // 处理完请求，返回内容
//        logger.info("RESPONSE : " + ret);
    }

//    @Around("webLog()")
//    public void around(ProceedingJoinPoint pjp) throws Throwable{
//        this.printLog("已经记录下操作日志@Around 方法执行前");
//        pjp.proceed();
//        this.printLog("已经记录下操作日志@Around 方法执行后");
//    }
}
